﻿using System;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Csla;

namespace Model
{ 
	[Serializable()] 
	public class KhachHangBOList : Csla.NameValueListBase<int?, string>
	{
		private static string whereDefaultKhachHangBOList = "";

		#region Factory Methods
		private KhachHangBOList()
		{  
		}

		public void InsertCustomValue(int? customKey, string customValue)
		{
			IsReadOnly = false;
			this.Insert(0, new NameValuePair(customKey, customValue));
			IsReadOnly = true;
		}
		
		public void CleaAll()
        {
            IsReadOnly = false;
            this.ClearItems();
            IsReadOnly = true;
        }
		
		private static KhachHangBOList _list;

		public static KhachHangBOList GetKhachHangBOList()
		{
			string where = "WHERE DELE=0" + (whereDefaultKhachHangBOList == "" ? "" : " AND " + whereDefaultKhachHangBOList);
			SPParam spParam = new SPParam("TenKhachHang", "ASC", where);
			_list = DataPortal.Fetch<KhachHangBOList>(spParam);
			return _list;
		}
		
		public static KhachHangBOList GetKhachHangBOListWithNullValue()
		{
			string where = "WHERE DELE=0" + (whereDefaultKhachHangBOList == "" ? "" : " AND " + whereDefaultKhachHangBOList);
			SPParam spParam = new SPParam("TenKhachHang", "ASC", where);
			_list = DataPortal.Fetch<KhachHangBOList>(spParam);
			_list.IsReadOnly = false;
			_list.Insert(0, new NameValuePair(null, " "));
			_list.IsReadOnly = true;
			return _list;
		}
		
		public static KhachHangBOList GetKhachHangBOListWithNullValue(string where)
		{
			where = "WHERE DELE=0" + (where == "" ? "" : " AND " + where) + (whereDefaultKhachHangBOList == "" ? "" : " AND " + whereDefaultKhachHangBOList);
			SPParam spParam = new SPParam("TenKhachHang", "ASC", where);
			_list = DataPortal.Fetch<KhachHangBOList>(spParam);
			_list.IsReadOnly = false;
			_list.Insert(0, new NameValuePair(null, " "));
			_list.IsReadOnly = true;
			return _list;
		}

		public static KhachHangBOList GetKhachHangBOList(bool getDeleted)
		{		
			string where = (whereDefaultKhachHangBOList == ""?"" : "WHERE " + whereDefaultKhachHangBOList);
			SPParam spParam = new SPParam("TenKhachHang", "ASC", where);
			_list = DataPortal.Fetch<KhachHangBOList>(spParam);
			return _list;
		}

		public static KhachHangBOList GetKhachHangBOList(string where)
		{
			where = "WHERE DELE=0" + (where == "" ? "" : " AND " + where) + (whereDefaultKhachHangBOList == "" ? "" : " AND " + whereDefaultKhachHangBOList);
			SPParam spParam = new SPParam("TenKhachHang", "ASC", where);
			_list = DataPortal.Fetch<KhachHangBOList>(spParam);
			return _list;
		}

		public static KhachHangBOList GetKhachHangBOList(string where, bool getDeleted)
		{
			string tmpWhere = ((where != "" || whereDefaultKhachHangBOList != "") ? "WHERE " : "");
            tmpWhere += (where == "" ? "" : where);
            if (whereDefaultKhachHangBOList != "")
                tmpWhere += (where == "" ? "" : " AND ") + whereDefaultKhachHangBOList;
			SPParam spParam = new SPParam("TenKhachHang", "ASC", tmpWhere);
			_list = DataPortal.Fetch<KhachHangBOList>(spParam);
			return _list;
		}

		public static KhachHangBOList GetKhachHangBOList(string where, string orderByColumn)
		{
			where = "WHERE DELE=0" + (where == "" ? "" : " AND " + where) + (whereDefaultKhachHangBOList == "" ? "" : " AND " + whereDefaultKhachHangBOList);
			SPParam spParam = new SPParam((orderByColumn == "" ? "TenKhachHang" : orderByColumn), "ASC", where);			
			_list = DataPortal.Fetch<KhachHangBOList>(spParam);
			return _list;
		}

		public static KhachHangBOList GetKhachHangBOList(string where, string orderByColumn, bool getDeleted)
		{
			string tmpWhere = ((where != "" || whereDefaultKhachHangBOList != "") ? "WHERE " : "");
			tmpWhere += (where == "" ? "" : where);
			if (whereDefaultKhachHangBOList != "")
				tmpWhere += (where == "" ? "" : " AND ") + whereDefaultKhachHangBOList;
			SPParam spParam = new SPParam((orderByColumn == "" ? "TenKhachHang" : orderByColumn), "ASC", tmpWhere);
			_list = DataPortal.Fetch<KhachHangBOList>(spParam);
			return _list;
		}

		public static KhachHangBOList GetKhachHangBOList(string where, string orderByColumn, string orderByExpression)
		{
			where = "WHERE DELE=0" + (where == "" ? "" : " AND " + where) + (whereDefaultKhachHangBOList == "" ? "" : " AND " + whereDefaultKhachHangBOList);
			SPParam spParam = new SPParam((orderByColumn == "" ? "TenKhachHang" : orderByColumn), (orderByExpression == "" ? "ASC" : orderByExpression), where);			
			_list = DataPortal.Fetch<KhachHangBOList>(spParam);
			return _list;
		}

		public static KhachHangBOList GetKhachHangBOList(string where, string orderByColumn, string orderByExpression, bool getDeleted)
		{
			string tmpWhere = ((where != "" || whereDefaultKhachHangBOList != "") ? "WHERE " : "");
			tmpWhere += (where == "" ? "" : where);
			if (whereDefaultKhachHangBOList != "")
				tmpWhere += (where == "" ? "" : " AND ") + whereDefaultKhachHangBOList;
			SPParam spParam = new SPParam((orderByColumn == "" ? "TenKhachHang" : orderByColumn), (orderByExpression == "" ? "ASC" : orderByExpression), tmpWhere);
			_list = DataPortal.Fetch<KhachHangBOList>(spParam);
			return _list;
		}
		
		public static void InvalidateCache()
		{
			_list = null;
		}
		#endregion Factory Methods
		//==========================================================================
		
		#region Data Access
		protected override void DataPortal_Fetch(object _spParam)
		{
			SPParam spParam = (SPParam)_spParam;
			RaiseListChangedEvents = false;
			SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
			SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("GetBOList");
			db.AddInParameter(cmd, "@TableName", SqlDbType.VarChar, (object)"KhachHang");
            db.AddInParameter(cmd, "@KeyColumn", SqlDbType.VarChar, (object)"KhachHangID");
            db.AddInParameter(cmd, "@ValueColumn", SqlDbType.VarChar, (object)"TenKhachHang");
            db.AddInParameter(cmd, "@WhereExpression", SqlDbType.NVarChar, (object)spParam.whereExpression);
            db.AddInParameter(cmd, "@OrderColumn", SqlDbType.VarChar, (object)spParam.orderColumn);
            db.AddInParameter(cmd, "@OrderExpression", SqlDbType.VarChar, (object)spParam.orderExpression);
			using (SqlDataReader dr = (SqlDataReader)db.ExecuteReader(cmd))
            {
				IsReadOnly = false;
                while (dr.Read())
					this.Add(new NameValuePair((int?)dr["KhachHangID"], (string)dr["TenKhachHang"]));
                IsReadOnly = true;
            }
			RaiseListChangedEvents = true;
		}
		#endregion Data Access
		//==========================================================================
	}
}
